﻿/************************************************
*
* Copyright(c) ZeeGuo.com
*
* CLR : 4.0.30319.296
*
* FileName : MainFormController.cs
*
* Author : Kurodo
*
* CreatTime : 2013/7/4 13:49:00
*           
*************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using ComicSpider;
using ComicSpider.Model;
using ComicSite;

namespace ComicHelper.Controller
{
    public class MainFormController
    {
        private HttpHelper http;
        private object objState = new object();
        private EntityComicBookList list;
        private List<EntityComicBook> booksList;
        //private EntityComicBook bookComics;
        private IComicStorage iStorage;
        private Queue<EntityComicBook> downIdxQueue;
        private IComicSite iSite;

        public int SiteNum
        {
            get;
            set;
        }
 
        public MainForm View
        {
            get;
            private set;
        }

        public MainFormController(MainForm view)
        {
            this.View = view;
            this.View.Controller = this;
        }

        public void GetComicSiteBookList(string url)
        {
            iSite = ComicFactory.CreateSite(this.SiteNum);

            http = http == null ? new HttpHelper() : http;
            http.Encoding = iSite.SiteEncode();
            string result = http.Request(url);
            list = list == null ? iSite.ComicList() : list;
            list.UrlAddress = url;
            list.EntityBuild(result);

            this.View.groupBox_List.Text = string.Format("{0} - {1}",this.View.groupBox_List.Text, list.Comic_BookName);
            this.booksList = list.GetComicList(result);

            int count = this.booksList.Count;
            int nums = count / 3;
            int devalue = count > 30 ? 30 % (nums * 3) + nums : count;
            //if (count > 30)
            //{
            //    devalue = 30 % (nums * 3) + nums;
            //}
            //else
            //    devalue = count;

            int i = 0;

            foreach (EntityComicBook b in booksList)
            {
                CheckBox box = new CheckBox();
                Point point = new Point();
                int intervalX = 120;
                int intervalY = 20;
                int q = i / devalue;
                point.X = intervalX * q + 10;
                point.Y = (i - devalue * q) * intervalY;

                box.Tag = i.ToString();
                box.Location = point;
                box.Text = b.Comic_BookName;
                this.View.panel_List.Controls.Add(box);
                i++;
            }
        }

        public void SaveSelectedComicBook()
        {
            this.iStorage = ComicFactory.CreateStorage(this.SiteNum);
            this.downIdxQueue = new Queue<EntityComicBook>();

            foreach (CheckBox check in this.View.panel_List.Controls)
            {
                if (check.Checked)
                {
                    int index = int.Parse(check.Tag.ToString());
                    EntityComicBook q = this.booksList[index];
                    GetTheCompleteComicBook(q);
                    this.downIdxQueue.Enqueue(q);
                    //MessageBox.Show(comiclist.Comic_PageCount.ToString());
                }
            }

            EntityComicBook first = this.downIdxQueue.Dequeue();
            this.DownLoadComicBook(first);
            //batch download
            //Thread thread = new Thread(new ThreadStart(DownloadComics));
            //thread.Start();
            //thread.Join();
        }

        private void GetTheCompleteComicBook(EntityComicBook comics)
        {
            string result = this.http.Request(comics.UrlAddress);
            comics.EntityBuild(result);
            comics.GenerateComicPages(result);
        }

        private bool IsNextComicBook()
        {
            return this.downIdxQueue.Count == 0 ? false : true;
        }

        private void DownLoadComicBook(EntityComicBook comics)
        {
            MultiDownLoader downer = new MultiDownLoader(comics.Comic_Pages);
            downer.MaxThreadCount = 1;
            downer.Storage = this.iStorage;
            downer.Folder = this.View.textBox_Folder.Text;
            this.View.foot_StatusLabel.Text = string.Format("{0} 开始下载!", comics.Comic_BookName);
            downer.DownloadFilesCompleted += delegate
            {
                this.View.foot_StatusLabel.Text = string.Format("{0} 下载完成!", comics.Comic_BookName);
                if (IsNextComicBook())
                {
                    EntityComicBook next = this.downIdxQueue.Dequeue();
                    this.DownLoadComicBook(next);
                }
                else
                    this.View.button_Analyze.Enabled = true;
            };

            downer.Start();
        }

        //private void DownloadComics()
        //{
        //    foreach (CheckBox check in this.View.panel_List.Controls)
        //    {
        //        if (check.Checked)
        //        {
        //            int index = int.Parse(check.Tag.ToString());
        //            this.bookComics = this.booksList[index];

        //            MultiDownLoader downer = new MultiDownLoader(this.bookComics.Comic_Pages);
        //            downer.MaxThreadCount = 1;
        //            downer.Storage = this.iStorage;
        //            downer.Folder = this.View.textBox_Folder.Text;
        //            this.View.foot_StatusLabel.Text = string.Format("{0} 开始下载!", this.bookComics.Comic_BookName);
        //            downer.DownloadFilesCompleted += delegate
        //            {
        //                this.View.foot_StatusLabel.Text = string.Format("{0} 下载完成!", this.bookComics.Comic_BookName);
        //            };

        //            downer.Start();
        //        }
        //    }
        //}
    }
}
